#include "camera.h"
#include "DirectXMath.h"
#include "Utility.h"

namespace mray{
	Camera::Camera(const vec3& pos, const vec3& tar, const vec3& up, float fov){
		m_pos = pos;
		m_look = (tar-pos).normalize();
		m_up = up;
		m_fov = fov;
		m_tanFov = std::tan(radian(m_fov));

		m_right = m_up % m_look;
		m_up = m_look % m_right;
		m_right.normalize();
		m_look.normalize();
		m_up.normalize();

	}

	mray::Ray Camera::CastRay(const vec2& uv)const
	{
		vec3 dir = m_look + m_right*uv.x*m_tanFov + m_up*uv.y*m_tanFov;
		return Ray(m_pos, dir.normalize());
	}

	void Camera::Move(const vec3& dir, float dis)
	{
		m_pos += dir*dis;
	}

	void Camera::Rotate(const vec3& n, float angle)
	{
		auto rot = DirectX::XMMatrixRotationNormal(n.xmvec, angle);
		m_up.xmvec = DirectX::XMPlaneTransform(m_up.xmvec, rot);
		m_look.xmvec = DirectX::XMPlaneTransform(m_look.xmvec, rot);
		m_right.xmvec = DirectX::XMPlaneTransform(m_right.xmvec, rot);
	}

}
